<?php
/*
 * @分销商城
 * @功能说明：商品管理中心
 * @更新说明：暂无更新
 * @文件名 GoodsController.class.php
 * @编码 UTF-8
 * @创建时间 2015-12-25 上午11:50:21
 * @创建人 XING
 */
namespace dg\Controller;
use Think\Controller;
class GoodsController extends ApibaseController {
	/*
	 * 分销商城_商品管理
	 * @date: 2015-12-25 上午11:50:41
	 * @editor: XING
	 */
    public function index(){
    	/*参数初始化*/
    	$page = I('get.page') ?: 1;
    	$perpage = 10;$url = '';
    	$list = M('GoodsEntity')->where(array('delete_at'=>'0'));
    	$url = array();
    	if(I('get.goods_title')){
    		$list = $list->where('goods_title LIKE "%'.I('get.goods_title').'%"');
    		$url['goods_title'] = I('get.goods_title');
    	}
    	if(in_array(I('get.vid'),array('1','2'))){
    		$list = $list->where(array('type'=>I('get.vid')));
    		$url['vid'] = I('get.vid');
    	}else{
    		$this->showtips('参数非法');
    	}
		if(I('get.status')!=''&&I('get.status')!='all'){
			$list = $list->where(array('status'=>I('get.status')));
			$url['status'] = I('get.status');
		}
		if(I('get.cate1_id')!=''&&I('get.cate1_id')!='all'){
			$list = $list->where(array('cate1_id'=>I('get.cate1_id')));
			$this->getCate = M('CateEntity')->where(array('parent_id'=>I('get.cate1_id')))->order(array('sort'=>'DESC'))->select();
			$url['cate1_id'] = I('get.cate1_id');
		}
		if(I('get.cate2_id')!=''&&I('get.cate2_id')!='all'){
			$list = $list->where(array('cate2_id'=>I('get.cate2_id')));
			$url['cate2_id'] = I('get.cate2_id');
		}
		if(I('get.kcun') && I('get.kcun') == '1'){
			$list = $list->where(array('kcun'=>'0'));
			$url['kcun'] = I('get.kcun');
		}else if(I('get.kcun') == '2'){
			$list = $list->where('kcun > 0');
			$url['kcun'] = I('get.kcun');
		}
    	$sObject = clone $list;
    	$getData = $list->order(array('add_at'=>'desc'))->page($page,$perpage)->select();
		$getLevels = M('CateEntity')
			->where(array('parent_id'=>'0'))->select();
		$this->assign('cate',$getLevels);
    	$this->assign('page',setPage($sObject->count(),$perpage,$page,'?'.http_build_query($url)));
    	$this->assign('getData',$getData);
  
    	$this->display('goods/index');
    }
    
	/*
	 * 分销商城_添加商品
	 * @date: 2015-12-25 下午12:16:16
	 * @editor: XING
	 */
    public function addGoods(){
    	if(I('post.dosubmit')){
    		if(I('post.goods_title') == ''){
    			$this->showtips('商品名称有误');
    		}
    		if(I('post.goods_price') <= '0'){
    			$this->showtips('商品价格有误');
    		}
    		if(I('post.goods_attr') <= '0'){
    			$this->showtips('商品规格有误');
    		}
    		if(I('post.goods_id')){
    			$effectId = M('GoodsEntity')->where(array('goods_id'=>I('post.goods_id')))
	    				  ->setField(array(
		    					'goods_title' => trim(I('post.goods_title')),
		    					'goods_price' => trim(I('post.goods_price')),
							  	'refer_price' => trim(I('post.refer_price')),
							    'goods_hb_price' => trim(I('post.goods_hb_price')),
							    'goods_full_price' => trim(I('post.goods_full_price')),
							    'goods_taxes_price' => trim(I('post.goods_taxes_price')),
							  	'kcun' => trim(I('post.kcun')),
							    'rate' => trim(I('post.ratesnum')),
							    'rete_column' => trim(I('post.rate')),
	    				  		'goods_pic' => trim(I('post.goods_pic')),
	    				  		'goods_pic2' => trim(I('post.goods_pic2')),
	    				  		'goods_pic3' => trim(I('post.goods_pic3')),
		    					'goods_attr' => trim(I('post.goods_attr')),
							    'hjprofit' => trim(number_format(I('post.hjprofit'),1,'.','')),
							    'byprofit' => trim(number_format(I('post.byprofit'),1,'.','')),
							    'zsprofit' => trim(number_format(I('post.zsprofit'),1,'.','')),
	    				  		'goods_content' => htmlspecialchars(I('post.content')),
	    				  		'cate1_id' => trim(I('post.cate1_id')),
	    				  		'cate2_id' => trim(I('post.cate2_id')),
							  	'taxes' => I('post.taxes'),
							  	'sort' => I('post.sort'),
							  	'status' => trim(I('post.status')),
		    					'update_at' => time()
		    			));
	    		M('ShopCart')
	    			->where(array('goods_id'=>I('post.goods_id')))->setField(array(
	    					'online' => trim(I('post.status'))
	    				));
    			if($effectId){
    				putlog('更新商品 `'.I('post.goods_title').'`','GoodsController','addGoods');
    				$this->showtips('商品信息更新成功');
    			}else{
    				$this->showtips('操作失败');
    			}
    		}
    		$addId = M('GoodsEntity')->add(array(
					'goods_title' => trim(I('post.goods_title')),
		    		'goods_price' => trim(I('post.goods_price')),
					'channel_price' => trim(I('post.channel_price')),
					'refer_price' => trim(I('post.refer_price')),
					'goods_hb_price' => trim(I('post.goods_hb_price')),
					'goods_full_price' => trim(I('post.goods_full_price')),
					'goods_taxes_price' => trim(I('post.goods_taxes_price')),
		    		'goods_attr' => trim(I('post.goods_attr')),
					'kcun' => trim(I('post.kcun')),
				    'rate' => trim(I('post.ratesnum')),
				    'rete_column' => trim(I('post.rate')),
    				'goods_pic' => trim(I('post.goods_pic')),
    				'goods_pic2' => trim(I('post.goods_pic2')),
    				'goods_pic3' => trim(I('post.goods_pic3')),
					'hjprofit' => trim(number_format(I('post.hjprofit'),1,'.','')),
					'byprofit' => trim(number_format(I('post.byprofit'),1,'.','')),
					'zsprofit' => trim(number_format(I('post.zsprofit'),1,'.','')),
    				'cate1_id' => trim(I('post.cate1_id')),
    				'cate2_id' => trim(I('post.cate2_id')),
					'status' => trim(I('post.status')),
    				'type' => trim(I('post.vid')),
					'taxes' => I('post.taxes'),
    				'goods_content' => htmlspecialchars(I('post.content')),
    				'add_at' => time(),
    				'update_at' => time()
    		));
    		if($addId){
    			putlog('添加商品 '.I('post.goods_title').' ID`'.$addId.'`','GoodsController','addGoods');
    			$this->showtips('添加商品成功');
    		}else{
    			$this->showtips('操作失败');
    		}
    	}
    	$getLevels = M('CateEntity')
    			   ->where(array('parent_id'=>'0'))->select();
    	$this->assign('cate',$getLevels);
		$this->assign('getRateArr',skplus());
    	if(I('get.goods_id')){
    		$getData = M('GoodsEntity')
    				 ->where(array('goods_id'=>I('get.goods_id')))->find();
    		$this->assign('lowPrice',round(($getData['channel_price']+5)/0.84,1));
    		$this->assign('getData',$getData);
    	}
    	$this->display('goods/addgoods');
    }
    
    /*
     * 分销商城_添加OEM
     * @date: 2015-12-25 下午12:16:16
     * @editor: XING
     */
    public function addOemGoods(){
    	if(I('post.dosubmit')){
    		if(I('post.goods_title') == ''){
    			$this->showtips('商品名称有误');
    		}
    		if(I('post.goods_price') <= '0'){
    			$this->showtips('商品价格有误');
    		}
    		if(I('post.goods_attr') <= '0'){
    			$this->showtips('商品规格有误');
    		}
    		if(I('post.goods_id')){
    			$effectId = M('GoodsEntity')->where(array('goods_id'=>I('post.goods_id')))
			    			->setField(array(
			    					'goods_title' => trim(I('post.goods_title')),
			    					'goods_price' => trim(I('post.goods_price')),
			    					'kcun' => trim(I('post.kcun')),
			    					'goods_pic' => trim(I('post.goods_pic')),
			    					'goods_pic2' => trim(I('post.goods_pic2')),
			    					'goods_pic3' => trim(I('post.goods_pic3')),
			    					'goods_attr' => trim(I('post.goods_attr')),
			    					'channel_price' => trim(I('post.channel_price')),
			    					'brand' => trim(I('post.brands')),
			    					'goods_content' => htmlspecialchars(I('post.content')),
			    					'cate1_id' => trim(I('post.cate1_id')),
			    					'resource' => '2',
			    					'cate2_id' => trim(I('post.cate2_id')),
			    					'sort' => I('post.sort'),
			    					'status' => trim(I('post.status')),
			    					'update_at' => time()
			    			));
    			M('ShopCart')
	    			->where(array('goods_id'=>I('post.goods_id')))->setField(array(
	    				'online' => trim(I('post.status'))
	    			));
    			if($effectId){
    				putlog('更新OEM商品 `'.I('post.goods_title').'`','GoodsController','addGoods');
    				$this->showtips('商品信息更新成功');
    			}else{
    				$this->showtips('操作失败');
    			}
    		}
    		$addId = M('GoodsEntity')->add(array(
    				'goods_title' => trim(I('post.goods_title')),
    				'goods_price' => trim(I('post.goods_price')),
    				'channel_price' => trim(I('post.channel_price')),
    				'goods_attr' => trim(I('post.goods_attr')),
    				'kcun' => trim(I('post.kcun')),
    				'goods_pic' => trim(I('post.goods_pic')),
    				'goods_pic2' => trim(I('post.goods_pic2')),
    				'goods_pic3' => trim(I('post.goods_pic3')),
    				'channel_price' => trim(I('post.channel_price')),
    				'brand' => trim(I('post.brands')),
    				'resource' => '2',
    				'cate1_id' => trim(I('post.cate1_id')),
    				'cate2_id' => trim(I('post.cate2_id')),
    				'status' => trim(I('post.status')),
    				'type' => trim(I('post.vid')),
    				'goods_content' => htmlspecialchars(I('post.content')),
    				'add_at' => time(),
    				'update_at' => time()
    		));
    		if($addId){
    			putlog('添加OEM商品 '.I('post.goods_title').' ID`'.$addId.'`','GoodsController','addGoods');
    			$this->showtips('添加商品成功');
    		}else{
    			$this->showtips('操作失败');
    		}
    	}
    	$getLevels = M('CateEntity')
    	->where(array('parent_id'=>'0'))->select();
    	$this->assign('cate',$getLevels);
    	if(I('get.goods_id')){
    		$this->assign('getRateArr',skplus());
    		$getData = M('GoodsEntity')
    		->where(array('goods_id'=>I('get.goods_id')))->find();
    		$this->assign('lowPrice',round(($getData['channel_price']+5)/0.84,1));
    		$this->assign('getData',$getData);
    	}
    	$this->display('goods/addoem');
    }
    
	/*
	 * 分销商城_商品删除
	 * @date: 2015-12-25 上午11:50:52
	 * @editor: XING
	 */
    public function deGoods(){
    	M('GoodsEntity')
    			->where(array('goods_id'=>I('get.goods_id')))->setField(array('delete_at'=>time()));
    	putlog('商品删除操作-'.session('loginUid').'/'.session('loginName'),'GoodsController','deGoods');
    	$this->showtips('删除成功');
    }
    
	/*
	 * 分销商城_分类AJAX
	 * @date: 2015-12-25 下午1:21:34
	 * @editor: XING
	 */
    public function cates(){
    	if(I('get.ajaxFrom')){
    		$cates = M('CateEntity')->where(array(
    				'parent_id' => I('get.cateid')
    			))->order(array('sort'=>'desc'))->select();
    	
    		jsonRespons($cates ?: 0);
    	}
    }
    
    /*
     * 分销商城_查询价格
     * @date: 2016-3-25 下午7:53:33
     * @editor: XING
     */
    public function gprice(){
    	if(I('get.price')){
    		$arrProfit = explode(',',getSysConfig('levelRate'));
    		echo json_encode(array('price1'=>$arrProfit[0]*I('get.price'),'price2'=>$arrProfit[1]*I('get.price'),'price3'=>$arrProfit[2]*I('get.price')));exit;
    	}
    }
    
    /*
	 * 分销商城_税款计算
	 * @date: 2016-5-25 下午3:19:57
	 * @editor: XING
	 */
    public function skplus(){
    	dataFormat(skplus(I('post.price') ?: 0,I('post.lm') ?: 0,I('post.taxes') ?: 0));
    }
}